public class Sort {
    // 插入排序
    public static void insertSort(int[] array){
        // write code  here
        //插入排序是把新摸得牌插入来原来的牌序中
        for(int i = 1; i < array.length; i++){
            int tmp = array[i];
            int j = 0;
            for(j = i - 1; j >= 0; j--){
                if(array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 希尔排序
    public static void shellSort(int[] array){
        // write code  here
        int gap = array.length;
        while(gap > 1){
            gap /= 2;
            shell(array, gap);
        }
    }
    private static void shell(int[] array, int gap){
        for(int i = 1; i < array.length; i++){
            int tmp = array[i];
            int j = 0;
            for (j = i - gap; j >= 0; j-=gap) {
                if(array[j] > tmp){
                    array[j + gap] = array[j];
                }else{
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    // 选择排序
    public static void selectSort(int[] array){
        // write code  here
        //找到最小的元素放在最前面
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }


    // 堆排序
    public static void heapSort(int[] array){
        // write code  here
        createHeap(array);
        for(int i = array.length - 1; i >= 0; i--){
            swap(array, 0, i);
            shiftDown(array, 0, i);
        }
    }
    private static void createHeap(int[] array){
        int parent = 0;
        for(parent = (array.length - 1) / 2; parent >= 0; parent--){
            shiftDown(array, parent, array.length);
        }
    }
    private static void shiftDown(int[] array, int parent, int len){
        int child = 2 * parent + 1;
        while(child < len){
            if(child + 1 < len && array[child] < array[child + 1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }
    // 冒泡排序
    public static void bubbleSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if(!flag){
                break;
            }
        }
    }

    // 快速
    public static void quickSort(int[] array){
        // write code  here
        int start = 0;
        int end = array.length - 1;
        quick(array, start, end);
    }
    private static void quick(int[] array, int start, int end){
        if(start >= end){
            return;
        }
        int base = partition(array, start, end);
        quick(array, start, base - 1);
        quick(array, base + 1, end);
    }
    private static int partition(int[] array, int start, int end){
        int tmp = array[start];
        int index = start;
        while(start < end){
            while(start < end && array[end] >= tmp){
                end--;
            }
            while(start < end && array[start] <= tmp){
                start++;
            }
            //此时start指向大于base的值，end指向小于base的值
            swap(array, start, end);
        }
        swap(array, index, start);
        return start;
    }



    private static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
